﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using PH.Model.Standards;
namespace PH.Data.Standards
{
    public class EvaluationStandardCommand
    {
        public static EvaluationStandardCollection Gets(EvaluationQuery sea)
        {
            var ret = new EvaluationStandardCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<Evaluation> q = db.DataContext.Evaluations;
                if (sea != null)
                {
                    if (sea.GradeCode > 0)
                        q = q.Where(p => p.GradeCode == sea.GradeCode);
                    if (sea.Sex > 0)
                        q = q.Where(p => p.Sex == sea.Sex);
                    if (sea.SubjectID > 0)
                        q = q.Where(p => p.SubjectID == sea.SubjectID);
                    if (sea.Additional != null)
                        q = q.Where(p => p.Additional == sea.Additional.Value);
                }
                var n = q.Count();
                if (n > 0)
                {
                    q = q.OrderBy(p => p.Sex).ThenByDescending(p => p.Score);
                    if (sea != null && sea.PageSize > 0)
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    var lst = q.ToList();
                    ret = Convertor.ConvertCollection<EvaluationModel, Evaluation, EvaluationStandardCollection>(lst);
                    ret.TotalRecordsCount = n;
                }
            }
            return ret;
        }

        static EvaluationStandardCollection _list;
        public static EvaluationStandardCollection List
        {
            get
            {
                if (_list == null || _list.TotalRecordsCount == 0)
                    _list = Gets(new EvaluationQuery() { Additional = false });
                return _list;
            }
        }


        static EvaluationStandardCollection _additionalList;
        public static EvaluationStandardCollection AdditionalList
        {
            get
            {
                if (_additionalList == null || _additionalList.TotalRecordsCount == 0)
                    _additionalList = Gets(new EvaluationQuery() { Additional = true });
                return _additionalList;
            }
        }
    }
}
